package com.hpf.community.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hpf.community.model.RoleAuthModel;
import com.hpf.community.service.IRoleAuthService;
import com.hpf.community.utils.CommonUtils;
import com.hpf.community.utils.PaginationResult;
import com.hpf.community.utils.StandardResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 *　　
 *   RoleAuth 控制器    角色权限表
 *
 *   @author huangpengfei
 *   @since 2020-04-04
 */

@RestController
public class RoleAuthController {
    private final Logger logger = LoggerFactory.getLogger(RoleAuthController.class);

    @Autowired
    public IRoleAuthService roleAuthService;

    /**
     * 获取分页列表
     *
     * @author : huangpengfei
     * @since : Create in 2020-04-04
     */
    @GetMapping("/roleAuth")
    public StandardResult selectPage(@ModelAttribute RoleAuthModel roleAuthModel, Integer pageSize, Integer pageNumber) {
		try {
			if (pageSize == null || pageNumber == null) {
				return StandardResult.faildCheck("缺少必要的分页参数！");
			}
	     	Page<RoleAuthModel> page = new Page<RoleAuthModel>(pageNumber, pageSize);
	     	Wrapper<RoleAuthModel> wrapper = new EntityWrapper<RoleAuthModel>(roleAuthModel);
	     	roleAuthService.selectPage(page, wrapper);
			return PaginationResult.ok(null, page.getRecords(), page.getTotal(), page.getPages());
		} catch (Exception e) {
			logger.error("异常信息:", e);
			return PaginationResult.faild(e);
		}
    }

	/**
     * 获取列表
     *
     * @author : huangpengfei
     * @since : Create in 2020-04-04
     */
    @GetMapping("/roleAuthList")
    public StandardResult selectList(@ModelAttribute RoleAuthModel roleAuthModel) {
		try {
            List<RoleAuthModel> authListByRole = CommonUtils.getAuthListByCurrentRole();
            return StandardResult.ok(null, authListByRole);
		} catch (Exception e) {
			logger.error("异常信息:", e);
			return StandardResult.faild(e);
		}
    }

     /**
        * 添加
      *
     * @author : huangpengfei
     * @since : Create in 2020-04-04
     */
    @PostMapping("/roleAuth")
    public StandardResult insert(@ModelAttribute RoleAuthModel roleAuthModel) {
		try {
            roleAuthService.insert(roleAuthModel);
            return StandardResult.ok();
        } catch (Exception e) {
            logger.error("异常信息:", e);
            return StandardResult.faild(e);
        }
    }

    /**
         * 修改
     *
     * @author : huangpengfei
     * @since : Create in 2020-04-04
     */
    @PutMapping("/roleAuth")
    public StandardResult updateById(@RequestBody RoleAuthModel roleAuthModel) {
        try {
            roleAuthService.updateById(roleAuthModel);
            return StandardResult.ok();
        } catch (Exception e) {
            logger.error("异常信息:", e);
            return StandardResult.faild(e);
        }
    }

    /**
        * 通过id获取详情
     *
     * @author : huangpengfei
     * @since : Create in 2020-04-04
     */
    @GetMapping("/roleAuth/{id}")
    public StandardResult selectById(@PathVariable String id) {
		try {
		     return StandardResult.ok(roleAuthService.selectById(id));
		} catch (Exception e) {
		     logger.error("异常信息:", e);
		     return StandardResult.faild(e);
		}
    }

    /**
     * 通过id删除数据
     *
     * @author : huangpengfei
     * @since : Create in 2020-04-04
     */
    @DeleteMapping("/roleAuth/{id}")
    public StandardResult deleteById(@PathVariable String id) {
        try {
            roleAuthService.deleteById(id);
            return StandardResult.ok();
        } catch (Exception e) {
            logger.error("异常信息:", e);
            return StandardResult.faild(e);
        }
    }
}
